<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!--********************************************************************
* 该示例需要引入
* UGCWasm (https://iclient.supermap.io/web/libs/ugcwasm/1.0.1/UGCWasmAll.js)
*********************************************************************-->
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title data-i18n="resources.title_ugcSmooth"></title>
    <style>
      body {
        margin: 0;
        overflow: hidden;
        background: #fff;
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
      }
      #toolbar {
        position: absolute;
        top: 50px;
        right: 10px;
        width: 280px;
        text-align: center;
        z-index: 100;
        border-radius: 4px;
      }
    </style>
  </head>

  <body>
    <div id="toolbar" class="panel panel-primary">
      <div class="panel-heading">
        <h5 class="panel-title text-center">线光滑</h5>
      </div>
      <div class="panel-body content">
        <input type="button" class="btn btn-default" value="光滑" onclick="search()" />
        <input type="button" class="btn btn-default" value="移除" onclick="clearLayer()" />
      </div>
    </div>
    <div id="map" style="width: 100%; height: 100%"></div>
    <script type="text/javascript" include="bootstrap-css" src="../js/include-web.js"></script>
    <script type="text/javascript" include="ugcwasm" src="../../dist/mapboxgl/include-mapboxgl.js"></script>
    <script>
      var map, popup, point, line;
      var host = window.isLocal ? window.server : 'https://iserver.supermap.io';
      var url = host + '/iserver/services/map-world/rest/maps/World/zxyTileImage.png?z={z}&x={x}&y={y}';
      function initMap(url) {
        return new Promise((resolve) => {
          map = new mapboxgl.Map({
            container: 'map',
            style: {
              version: 8,
              sources: {
                'raster-tiles': {
                  type: 'raster',
                  tiles: [url],
                  tileSize: 256
                }
              },
              layers: [
                {
                  id: 'simple-tiles',
                  type: 'raster',
                  source: 'raster-tiles'
                }
              ]
            },
            center: [123, 32],
            zoom: 6
          });
          map.on('load', function () {
            resolve(map);
          });
        });
      }
      function addFeatures() {
        line = {
          geometry: {
            type: 'LineString',
            coordinates: [
              [123.58553734150553, 32.16247150999084],
              [123.31066843972036, 32.27874122071802],
              [122.72657202343038, 32.307785402609724],
              [122.38298589619893, 32.00721419190167],
              [122.6464019270752, 31.46174386499665],
              [122.98998805430665, 31.129004118740795],
              [122.8754593452295, 30.804930818841328],
              [122.23409857439981, 30.706509979514166],
              [121.5354734490304, 31.079972856746267],
              [121.71871938355378, 31.393334706846716],
              [121.74162512536924, 31.90032194653712],
              [121.38658612723123, 32.37551895758594],
              [120.55052655097165, 32.09457888936224],
              [120.5619794218781, 31.530103082191943],
              [120.90556554910955, 30.8737655015629],
              [120.2412990364644, 30.667113490029294],
              [119.51976816927959, 31.079972856746267],
              [119.03874759115786, 31.50081239394727],
              [118.88986026935646, 30.726202194317224]
            ]
          },
          type: 'Feature'
        };
        addLayer('line', 'line', [line], {
          'line-color': 'blue',
          'line-width': 2
        });
      }

      function search() {
        clearLayer();
        let distance;
        var geometryAnalysis = new mapboxgl.supermap.GeometryAnalysis();
        distance = geometryAnalysis.smooth(line, point);
        addLayer('line1', 'line', [distance], {
          'line-color': 'green',
          'line-width': 2
        });
      }

      function addLayer(id, type, sourceData, paint) {
        map.addLayer({
          id,
          type,
          source: {
            type: 'geojson',
            data: {
              type: 'FeatureCollection',
              features: sourceData
            }
          },
          paint
        });
      }


      function clearLayer() {
        if (map.getLayer('line1')) {
          map.removeLayer('line1');
          map.removeSource('line1');
        }
      }

      initMap(url).then((map) => {
        addFeatures();
      });
    </script>
  </body>
</html>
